<?php

include '../config/config.php';
include 'Zend/Db.php';

include 'get_feed_test.php';

$db = Zend_Db::factory('Pdo_Mysql', $dbConfig);

function get_modules($db) {	
	
	$query = 'SELECT module_code
			  FROM modules
	          ORDER BY module_code';
	
	$result = $db->fetchAll($query);
	
	return $result;	
}

function blogs_by_module($db) {	
	
	$query = 'SELECT blog_id, feedurl, module_id, module_code
			  FROM blogs, modules, tutorials, students
			  WHERE module_id = fk_module_id 
	          AND tutorial_id = fk_tutorial_id
	          AND student_id = fk_student_id
	          ORDER BY module_code';
	
	$result = $db->fetchAll($query);
	
	return $result;
	
}

$modules = get_modules($db);
$result = blogs_by_module($db);

$text = '';
$mod = array();

// build the $mod hash;
foreach ($modules as $mkey=>$mvalue) {
	
	$text = '';
	$module = $mvalue['module_code'];
	
	foreach ($result as $key=>$value) {
		
		// pull the blog
		$blog = get_feed_array ($value['feedurl']);
		
		if ($value['module_code'] == $module) {
			
			foreach ($blog['entries'] as $k=>$v) {
				$text .= $v['title'];
				$text .= $v['description'];
				$text .= $v['content'];
			}
		}
		else {
			continue;
		}
	}
	
	$mod[$module] = $db->quote($text);
}

// empty the table
$n = $db->delete('tags_by_module');

// insert the data
foreach ($mod as $key=>$value){
	
	$data = array(
	'tbm_id' => '',
	'fk_module_id' => $key,
	'tag_text'  => $value,
	'cleaned_text' => ''
	);
	
	$where[] = "fk_module_id LIKE '$key'";
	$n = $db->insert('tags_by_module', $data, $where);
	
}

$n = $db->delete('tags_by_module', 'tag_text="\'\'"');
echo "done";

?>